package com.xyz.tasks.bf;

import com.free.ext.logger.FreeLogger;
import com.free.ext.logger.FreeLoggerFactory;
import com.xyz.dao.logic.SLotterydetailLogic;
import com.xyz.domain.table.SLottery;
import com.xyz.domain.table.SLotterydetail;
import com.xyz.lottery.LotteryProbablity;
import com.xyz.lottery.Lotteryer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LotteryProbalityBf {

    @Autowired
    SLotterydetailLogic lotterydetailLogic;

    private static final FreeLogger LOG = FreeLoggerFactory.getLogger(LotteryProbalityBf.class);

    public static final Map<String, Lotteryer> LOTTERYERS = new HashMap<>();

    public void mkLotteryer(SLottery lotteryInfo){
        SLotterydetail cond = new SLotterydetail();
        cond.setLotteryId(lotteryInfo.getId());
        List<SLotterydetail> detailInfos = lotterydetailLogic.list(cond, StringUtils.EMPTY);
        if(detailInfos == null || detailInfos.size() == 0){
            LOG.info("抽奖CODE：{}的抽奖游戏，没有对应的详情信息",lotteryInfo.getCode());
            return;
        }
        List<LotteryProbablity> probablities = getLotteryProbalities(detailInfos);
        LOTTERYERS.put(lotteryInfo.getCode(), Lotteryer.instance(probablities, BigDecimal.valueOf(100)));
    }

    public Lotteryer getLotteryerByCode(String lotteryCode){
        return LOTTERYERS.get(lotteryCode);
    }

    private List<LotteryProbablity> getLotteryProbalities(List<SLotterydetail> detailInfos){
        List<LotteryProbablity> probablities = new ArrayList<>();
        detailInfos.forEach(x->{
            LotteryProbablity probablity = new LotteryProbablity(x.getId(),x.getProbability(),String.valueOf(x.getSort()));
            probablities.add(probablity);
        });
        return probablities;
    }


}
